//
// Created by Incredible on 17/3/15.
//

#ifndef SELECTIONSORT_SORTTESTHELPER_H
#define SELECTIONSORT_SORTTESTHELPER_H

#include <iostream>
#include <cassert>

using namespace std;

namespace SortTestHelper {

    //生成有n个元素的随机数组，每个随机元素的随机范围未[rangL, rangR]
    int *generateRandomArray(int n, int rangeL, int rangeR) {

        assert(rangeL <= rangeR);

        int *arr = new int[n];
        srand(time(NULL));

        //控制随机数生成范围
        for (int i = 0; i < n; ++i) {
            arr[i] = rand() % (rangeR - rangeL + 1) + rangeL;
        }

        return arr;
    }

    //打印数组
    template<typename T>
    void printArray(T arr[], int n) {
        for (int i = 0; i < n; ++i) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }

    //判断是否排序成功
    template<typename T>
    bool isSort(T arr[], int n) {
        for (int i = 0; i < n - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                return false;
            }
            return true;
        }
    }

    //testSort（计算消耗时间并判断是否排序成功）
    template<typename T>
    void testSort(string sortName, void(*sort)(T[], int), T arr[], int n) {
        clock_t startTime = clock(); //时钟周期数
        sort(arr, n);
        clock_t endTime = clock();

        assert(isSort(arr, n));

        cout << sortName << " : " << double(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    }

    //生成近乎有序的数组
    int *generateNearOrderedArray(int n, int swapTime) {
        int *a = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = i;
        }

        //随机交换有序数组的元素，打乱数组
        srand(time(NULL));
        for (int j = 0; j < swapTime; j++) {
            int posX = rand() % n;
            int posY = rand() % n;

            swap(a[posX], a[posY]);
        }

        return a;
    }


    //拷贝int *数组
    int *copyIntArray(int a[], int n) {

        int *arr = new int[n];
        //内存拷贝（起始，结束，新起始）
        copy(a, a + n, arr);
        return arr;
    }

}

#endif //SELECTIONSORT_SORTTESTHELPER_H
